//
//  ViewController.m
//  MultipleLevelPrj
//
//  Created by apple on 2017/9/25.
//  Copyright © 2017年 itheima. All rights reserved.
//

#import "ViewController.h"

@interface BaseModel : NSObject
@property(nonatomic, strong) NSString *name;
@end
@implementation BaseModel
@end

@interface VideoModel : BaseModel
@end
@implementation VideoModel
@end

@interface PointModel : BaseModel
@property(nonatomic, strong) NSMutableArray<VideoModel*> *Video;
@end
@implementation PointModel
+(NSDictionary*)modelContainerPropertyGenericClass{
    return @{@"Video" : [VideoModel class]};
}
@end

@interface SectionModel : BaseModel
@property(nonatomic, strong) NSMutableArray<PointModel*> *Point;
@end
@implementation SectionModel
+(NSDictionary*)modelContainerPropertyGenericClass{
    return @{@"Point" : [PointModel class]};
}
@end

@interface ChapterModel : BaseModel
@property(nonatomic, strong) NSMutableArray<SectionModel*> *Section;
@end
@implementation ChapterModel
+(NSDictionary*)modelContainerPropertyGenericClass{
    return @{@"Section" : [SectionModel class]};
}
@end

@interface BookModel : BaseModel
@property(nonatomic, strong) NSMutableArray<ChapterModel*> *Chapter;
@end
@implementation BookModel
+(NSDictionary*)modelContainerPropertyGenericClass{
    return @{@"Chapter" : [ChapterModel class]};
}
@end

@interface DataSource : NSObject
@property(nonatomic, strong) id model;
//@property(nonatomic, assign) BOOL isDisplay;
@property(nonatomic, assign) BOOL isExpand;
@property(nonatomic, assign) NSInteger level;
@property(nonatomic, strong) NSMutableArray<DataSource*> *arrNode;
@end
@implementation DataSource
@end

@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, strong) DataSource *dataSource;
@property(nonatomic, strong) NSMutableDictionary<NSNumber */*key=section*/, NSMutableArray<DataSource*> *> *dictReloadDataSource; //已载入的数据源
@property(nonatomic, strong) NSMutableArray<DataSource*> *arrTempDataSource; //已载入的数据源
@property(nonatomic, strong) UITableView *tableView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSString *plistPath  = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    BookModel *model = [BookModel yy_modelWithDictionary:dict];
    if(!model) {
        NSLog(@"model is nil");
        return ;
    }
    //创建数据源
    _dataSource = [self createDataSource:model];
    _arrTempDataSource = [NSMutableArray new];
    [_arrTempDataSource addObject:_dataSource];
    _dictReloadDataSource = [NSMutableDictionary new];
    [_dictReloadDataSource setObject:_arrTempDataSource forKey:[NSNumber numberWithInteger:0]];
    
    //数据展示 UITableView
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame
                                              style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [UIView new];
    [self.view addSubview:_tableView];
}

-(DataSource*)createDataSource:(id)model
{
    DataSource *ds = [[DataSource alloc] init];
    ds.model = model;
    if([model isKindOfClass:[BookModel class]]) {
        ds.isExpand = NO;
        ds.level=1;
        if( ((BookModel*)model).Chapter ) {
            ds.arrNode = [[NSMutableArray alloc] init];
            for(ChapterModel *modelItem in ((BookModel*)model).Chapter) {
                DataSource *subDS = [self createDataSource:modelItem];
                [ds.arrNode addObject:subDS];
            }
        }
    } else if([model isKindOfClass:[ChapterModel class]]) {
        ds.isExpand = NO;
        ds.level=2;
        if( ((ChapterModel*)model).Section ) {
            ds.arrNode = [[NSMutableArray alloc] init];
            for(SectionModel *modelItem in ((ChapterModel*)model).Section) {
                DataSource *subDS = [self createDataSource:modelItem];
                [ds.arrNode addObject:subDS];
            }
        }
    } else if([model isKindOfClass:[SectionModel class]]) {
        ds.isExpand = NO;
        ds.level=3;
        if( ((SectionModel*)model).Point ) {
            ds.arrNode = [[NSMutableArray alloc] init];
            for(PointModel *modelItem in ((SectionModel*)model).Point) {
                DataSource *subDS = [self createDataSource:modelItem];
                [ds.arrNode addObject:subDS];
            }
        }
    } else if([model isKindOfClass:[PointModel class]]) {
        ds.isExpand = NO;
        ds.level=4;
        if( ((PointModel*)model).Video ) {
            ds.arrNode = [[NSMutableArray alloc] init];
            for(VideoModel *modelItem in ((PointModel*)model).Video) {
                DataSource *subDS = [self createDataSource:modelItem];
                [ds.arrNode addObject:subDS];
            }
        }
    } else if([model isKindOfClass:[VideoModel class]]) {
        ds.isExpand = NO;
        ds.level=5;
    }
    return ds;
}

-(NSInteger)numberOfRowsInDataSource:(DataSource*)dataSource{
    NSInteger numberOfRowsInDataSource = 0;
    if(dataSource.isExpand) {
        numberOfRowsInDataSource = 1;
        if(dataSource.arrNode) {
            for(DataSource *dataSourceItem in dataSource.arrNode) {
                numberOfRowsInDataSource += [self numberOfRowsInDataSource:dataSourceItem];
            }
        }
    } else {
        numberOfRowsInDataSource = 1;
    }
    return numberOfRowsInDataSource;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;              // Default is 1 if not implemented
//{
//    return _dataSource.arrNode.count;//数名
//}
//
//- (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;    // fixed font
//{
//    return ((BaseModel*)_dataSource.arrNode[section].model).name;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; {
    NSMutableArray *arr = self.dictReloadDataSource[[NSNumber numberWithInteger:section]];
    if(arr) {
        return  [self numberOfRowsInDataSource:arr[0]];
    }
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; {
    static NSString *cellID = @"cellID";
    UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(!tableViewCell) {
        tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    NSMutableArray *arr = self.dictReloadDataSource[[NSNumber numberWithInteger:indexPath.section]];
    DataSource *ds = arr[indexPath.row];
    tableViewCell.textLabel.text = ((BaseModel*)ds.model).name;
    return tableViewCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *arr = self.dictReloadDataSource[[NSNumber numberWithInteger:indexPath.section]];
    DataSource *ds = arr[indexPath.row];
    if(!ds.arrNode && ds.arrNode.count<=0) {
        return ;//叶子节点
    }

//    NSRange range = {indexPath.row+1, arr.count-(indexPath.row+1)};
//    [arr removeObjectsInRange:range];
//    [_arrTempDataSource removeAllObjects];
    
    if(ds.isExpand) {
        [self foldSectionWithDataSource:ds atIndexPath:indexPath];
    } else {
        [self expandSectionWithDataSource:ds atIndexPath:indexPath];
    }
    ds.isExpand = !ds.isExpand;
    
    [_tableView reloadData];
}

-(void)foldSectionWithDataSource:(DataSource*)ds atIndexPath:(NSIndexPath*)indexPath {
    if(!ds.arrNode && ds.arrNode.count<=0) {
        return ;//叶子节点
    }
    
    NSMutableArray *arr = self.dictReloadDataSource[[NSNumber numberWithInteger:indexPath.section]];
    //    NSMutableArray *tempArray = [NSMutableArray new];
    for(DataSource *dsItem in ds.arrNode) {
        [arr removeObject:dsItem];
        if(dsItem.isExpand) {
            [self foldSectionWithDataSource:dsItem atIndexPath:indexPath];
        }
    }
}

-(void)expandSectionWithDataSource:(DataSource*)ds atIndexPath:(NSIndexPath*)indexPath {
    if(!ds.arrNode && ds.arrNode.count<=0) {
        return ;//叶子节点
    }
    
    NSMutableArray *arr = self.dictReloadDataSource[[NSNumber numberWithInteger:indexPath.section]];
//    NSMutableArray *tempArray = [NSMutableArray new];
    NSInteger row = indexPath.row;
    for(DataSource *dsItem in ds.arrNode) {
        row++;
        [arr insertObject:dsItem atIndex:row];
        if(dsItem.isExpand) {
            NSIndexPath *subIndexPath = [NSIndexPath indexPathForItem:row inSection:indexPath.section];
            [self expandSectionWithDataSource:dsItem atIndexPath:subIndexPath];
        }
    }
}

// 章/节/点/视频
/*
 1: plist 构建数据源
//*/

@end
